<layout:basic/>
<div class="panel panel-default">
    <div class="panel-body">
        <button class="btn btn-default" id="oneditonline"><i class="fa fa-edit"></i> 在线编辑</button>
        <button class="btn btn-primary" id="getdevices">获取设备列表</button>

        <button class="btn btn-success" id="buildrelease">编译</button>
        <button class="btn btn-success" id="downloadrelease">下载APK</button>

        <hr>
        <table class="table table-bordered">
            <caption>设备列表</caption>
            <thead>
            <tr>
                <th>名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="devices_list"></tbody>
        </table>
    </div>
</div>

<script>
    $(function () {
        $("#oneditonline").click(function () {
            window.open("<?php echo L_PUBLIC_FULL_URL;?>/admin/index/authexplorer?dir4explore=<?php
                echo urlencode(L_PATH_BASE) . 'www';
                ?>");
        });

        $("#buildrelease").click(function () {
            var index = layer.alert('正在编译，请耐心等待');
            $.get("__PUBLIC__/admin/app/buildandroid", function (res) {
                layer.close(index);
                layer.alert(res.message);
            });
        });

        $("#downloadrelease").click(function () {
            window.open("__PUBLIC__/admin/app/downloadrelease");
        });

        var reloadDeviceList = function () {
            $.get("__PUBLIC__/admin/app/listDevices", function (res) {
                var html = "";
                for (var x in res.data) {
                    html += '<tr><td>' + res.data[x] + '</td><td>' +
                        '<button class="btn btn-default btn-sm run" data-device="' + res.data[x] + '">运行</button> ' +
                        '<button class="btn btn-primary btn-sm install" data-device="' + res.data[x] + '">安装</button> ' +
                        '<button class="btn btn-danger btn-sm uninstall" data-device="' + res.data[x] + '">卸载</button> ' +
                        '</td></tr>';
                }
                $("#devices_list").html(html);
                $(".run").click(function () {
                    var index = layer.alert('安装正在进行，请保持手机解锁');
                    $.get("__PUBLIC__/admin/app/runandroid", {
                        device: $(this).attr("data-device")
                    }, function (res) {
                        layer.close(index);
                        layer.alert(res.message);
                    });
                });
                $(".install").click(function () {
                    $.get("__PUBLIC__/admin/app/installandroid", {
                        device: $(this).attr("data-device")
                    }, function (res) {
                        layer.alert(res.message);
                    });
                });
                $(".uninstall").click(function () {
                    $.get("__PUBLIC__/admin/app/uninstallandroid", {
                        device: $(this).attr("data-device")
                    }, function (res) {
                        layer.alert(res.message);
                    });
                });
            });

        };

        $("#getdevices").click(reloadDeviceList);
        reloadDeviceList();

    });
</script>